package org.jctc.els.modules.biz.service.elsTeamProject.impl;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.biz.entity.elsTeamProject.ElsTeamProject;
import org.jctc.els.modules.biz.mapper.elsTeamProject.ElsTeamProjectMapper;
import org.jctc.els.modules.biz.mapper.elsUserProject.ElsUserProjectMapper;
import org.jctc.els.modules.biz.service.elsTeamProject.ElsTeamProjectService;
import org.jctc.els.modules.biz.vo.ElsTeamProjectVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author maxinyu
 * @title: elsTeamProjectServiceImpl
 * @projectName evaluationsystem-boot
 * @description:
 * @date 2022/6/2414:39
 */
@Service
@AllArgsConstructor
public class ElsTeamProjectServiceImpl  extends BaseServiceImpl<ElsTeamProjectMapper, ElsTeamProject> implements ElsTeamProjectService {

    @Autowired
    ElsTeamProjectMapper elsTeamProjectMapper;

    @Autowired
    private ElsUserProjectMapper elsUserProjectMapper;

    @Override
    public int selectProjectAlreadyExists(Long projectId, Long teamId) {
        return elsTeamProjectMapper.selectProjectAlreadyExists(projectId,teamId);
    }

    @Override
    public IPage<ElsTeamProjectVO> getProjectsList(IPage<ElsTeamProjectVO> page, String teamId) {
        return page.setRecords(elsTeamProjectMapper.getProjectsList(page,teamId));
    }

    @Override
    public void removeListByIds(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            elsTeamProjectMapper.removeListByIds(ids.get(i));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTeamProjects(Long teamId, List<Long> projectIds) {
        //删除小组中负责的专家--删除与此项目绑定的专家下的此项目
        elsUserProjectMapper.removeTeamProjectsMember(teamId, projectIds);
        //删除小组中的项目
        boolean result = elsTeamProjectMapper.removeTeamProjects(teamId, projectIds) > 0;
        if(BooleanUtil.isFalse(result)){
            throw new ServiceException("操作失败");
        }
        return result;
    }
}
